import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 通用确认对话框
///
/// 参数说明：
/// - [onConfirmed]: 确认按钮回调
/// - [onCanceled]: 取消/关闭回调
/// - [title]: 对话框标题（默认'提示'）
/// - [content]: 提示内容（默认'返回后所填信息不支持继续编辑，确定退出？'）
///
/// 使用示例：
/// ```dart
/// Get.dialog(
///   ExitTips(
///     onConfirmed: () => handleConfirm(),
///     title: '退出确认',
///     content: '确定要放弃当前编辑的内容吗？',
///   ),
/// );
/// ```
class ExitTips extends StatelessWidget {
  const ExitTips({
    required this.onConfirmed, super.key,
    this.title,
    this.content,
    this.onCanceled,
  });

  final String? title;
  final String? content;
  final VoidCallback onConfirmed;
  final VoidCallback? onCanceled;

  // 提取常用颜色为常量
  static const _accentColor = Color(0xff5C6DFF);
  static const _gradientColors = [
    Color(0xffE7EAFF),
    Colors.white,
    Colors.white,
    Colors.white,
  ];

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      insetPadding: EdgeInsets.symmetric(horizontal: 20.w),
      child: ConstrainedBox(
        constraints: BoxConstraints(maxWidth: 300.w),
        child: IntrinsicHeight(
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20.w),
              gradient: const LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: _gradientColors,
              ),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // 头部关闭按钮
                Align(
                  alignment: Alignment.topRight,
                  child: _CloseButton(
                    onTap: () {
                      onCanceled?.call();
                      Navigator.pop(context);
                    },
                  ),
                ),

                // 主要内容区域
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        title ?? '提示',
                        style: Theme.of(context).textTheme.titleLarge?.copyWith(
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 12.h),
                      Text(
                        content ?? '返回后所填信息不支持继续编辑，确定退出？',
                        textAlign: TextAlign.center,
                        style: Theme.of(context).textTheme.bodyMedium,
                      ),
                      SizedBox(height: 24.h),
                      // 操作按钮
                      _ActionButton(
                        text: '确定',
                        onPressed: () {
                          onConfirmed();
                          Navigator.pop(context);
                        },
                      ),
                      SizedBox(height: 16.h),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// 自定义关闭按钮组件
class _CloseButton extends StatelessWidget {
  const _CloseButton({required this.onTap});

  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return IconButton(
      iconSize: 30.w,
      padding: EdgeInsets.all(8.w),
      icon: Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
        ),
        child: Icon(
          Icons.close,
          size: 21.w,
          color: Colors.grey[600],
        ),
      ),
      onPressed: onTap,
    );
  }
}

/// 统一操作按钮样式
class _ActionButton extends StatelessWidget {
  const _ActionButton({
    required this.text,
    required this.onPressed,
  });

  final String text;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: 40.h,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: ExitTips._accentColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(24.w),
          ),
        ),
        onPressed: onPressed,
        child: Text(
          text,
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}